<?php

namespace App\Service\Admin;

use App\Model\Admin\GradeModel;
use App\Model\Admin\WorkerGradeRelModel;
use App\Service\BaseService;

/**
 * 年级管理-服务类
 * @author LZH
 * @since: 2021/08/12
 * Class GradeService
 */
class GradeService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new GradeModel();
        parent::__construct();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2021/08/12
     * @author LZH
     */
    public function getList()
    {
        $param = $this->getRequestParam();

        // 查询条件
        $map = [];


        return parent::getList($map);
    }

    public function edit()
    {
        $data = $this->getRequestParam();
        $temp = explode(':', $data['dept_type']);
        $data['dept_type'] = $temp[0];
        $data['dept_name'] = $temp[1];
        $id = parent::edit($data);
        /*
         Array
            (
                [0] => Array
                    (
                        [worker_id] => 1
                        [duty_code] => 4
                        [key] => 1629101607205
                    )
            )

        */
        $dynamicItem = $this->getRequestParam('dynamicItem'); // 保存到worker_grade_rel表中

        foreach ($dynamicItem as &$item) {
            $item['type'] = 0; // 年级和职工关系
            $item['type_id'] = $id;
            $temp = explode(':', $item['duty_code']);
            $item['duty_name'] = $temp[1];
            $item['duty_code'] = $temp[0];
        }

        $workerRoleRelModel = new WorkerGradeRelModel();
        $result = $workerRoleRelModel->saveAll($dynamicItem);
        return array_keys($result);
    }


    /**
     * 获取级数，即届数
     * @return array
     * @author LZH
     * @since 2021/08/16
     */
    public function getSchoolYear(): array
    {
        $schoolYears = [];
        $thisYear = date('Y');
        if (date('m') > 7) {
            $schoolYears[] = ['name' => $thisYear . '级', 'value' => $thisYear];
        }
        for ($year = date('Y') - 1; $year >= 2015; $year--) {
            $schoolYears[] = ['name' => $year . '级', 'value' => $year];
        }

        return $schoolYears;
    }

    /**
     * 获取学部
     * @param string $dicType 字典类型
     * @author LZH
     * @since 2021/08/16
     */
    public function getDeptType($dicType)
    {
        $dictTypeService = new DictTypeService();
        $result = $dictTypeService->getDictionaryByDictType($dicType);
        return $result;
    }

    public function getClassesGroupByGrade()
    {
        $result = [];
        $grades = $this->model->join('sc_classes', 'sc_classes.grade_id = sc_grade.id')
            ->field(['sc_grade.id as grade_id', 'sc_grade.name as grade_name', 'sc_classes.id as classes_id', 'sc_classes.name as classes_name'])
            ->where('sc_grade.mark', 1)
            ->all()
            ->toArray(true, false);

        array_walk($grades, function ($item) use (&$result) {
            $result[$item['grade_id']]['classes'][] = [
                'id' => $item['classes_id'],
                'name' => $item['classes_name']
            ];
            $result[$item['grade_id']]['name'] = $item['grade_name'];

//            $result[] = ['name' => $item['grade_name'], 'classes' => [[
//                'id' => $item['classes_id'],
//                'name' => $item['classes_name']
//            ]]];
//
//            $result[]['name'] = $item['grade_name'];
//            $result[]['classes'][] = [
//                'id' => $item['classes_id'],
//                'name' => $item['classes_name']
//            ];
        });

        print_r($result);
        return [array_values($result)];
    }
}
